package stream;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

public class MapUpdate extends JInternalFrame {
	private JTextField inId;
	private JButton btnSearch;
	private JTextField inName;
	private JTextField inPhone;
	private JButton btnMod;
	private JButton btnDel;
	private JTextField result;
	private JLabel lblNewLabel;
	private JLabel lblName;
	private JLabel lblPhone;
	private JLabel lblStatus;

	HashMap<String, MapData> uhm;
	
	public MapUpdate(HashMap<String, MapData> a) {
		super("Update", true, true, true, true);
		
		
		this.uhm=a;
		
		
		setTitle("Update");
		getContentPane().setBackground(Color.ORANGE);
		setBounds(100, 100, 386, 175);
		getContentPane().setLayout(null);
		getContentPane().add(getInId());
		getContentPane().add(getBtnSearch());
		getContentPane().add(getInName());
		getContentPane().add(getInPhone());
		getContentPane().add(getBtnMod());
		getContentPane().add(getBtnDel());
		getContentPane().add(getResult());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblName());
		getContentPane().add(getLblPhone());
		getContentPane().add(getLblStatus());

	}

	private JTextField getInId() {
		if (inId == null) {
			inId = new JTextField();
			inId.setBounds(85, 11, 116, 21);
			inId.setColumns(10);
		}
		return inId;
	}
	
	private JTextField getInName() {
		if (inName == null) {
			inName = new JTextField();
			inName.setBounds(85, 42, 116, 21);
			inName.setColumns(10);
		}
		return inName;
	}
	private JTextField getInPhone() {
		if (inPhone == null) {
			inPhone = new JTextField();
			inPhone.setBounds(85, 73, 116, 21);
			inPhone.setColumns(10);
		}
		return inPhone;
	}
	private JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("Search");
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(inId.getText().equals("")){
						JOptionPane.showMessageDialog(null, "해당 정보가 존재하지 않습니다.");
						return;
					}				
					
					try{						
						inName.setText(uhm.get(inId.getText()).irum);
						inPhone.setText(uhm.get(inId.getText()).phone);
						result.setText("조회되었습니다.");
						btnMod.setEnabled(true);
						btnDel.setEnabled(true);
					}catch(Exception ex){
						JOptionPane.showMessageDialog(null, "해당 정보가 존재하지 않습니다1.");
					}
					
					
					
				}
			});
			btnSearch.setBounds(213, 10, 116, 23);
		}
		return btnSearch;
	}
	
	private JButton getBtnMod() {
		if (btnMod == null) {
			btnMod = new JButton("Modify");
			btnMod.setEnabled(false);
			btnMod.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(inId.getText().equals("") || inName.getText().equals("") || inPhone.getText().equals("")){
						JOptionPane.showMessageDialog(null, "공백은 저장할 수 없습니다.");
						return;
					}
					
					MapData temp = new MapData(inId.getText(), inName.getText(), inPhone.getText());
					uhm.put(inId.getText(), temp);
					result.setText("저장되었습니다.");
					
					inId.setText("");
					inName.setText("");
					inPhone.setText("");
					btnMod.setEnabled(false);
					btnDel.setEnabled(false);
					
					
				}
			});
			btnMod.setBounds(213, 41, 116, 23);
		}
		return btnMod;
	}
	
	private JButton getBtnDel() {
		if (btnDel == null) {
			btnDel = new JButton("Delete");
			btnDel.setEnabled(false);
			btnDel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int b = JOptionPane.showConfirmDialog(null, "정말 삭제하시겠습니까?");
					if( b == JOptionPane.YES_OPTION){
						if(inId.getText().equals("")){
							JOptionPane.showMessageDialog(null, "해당 정보가 존재하지 않습니다.");
							return;
						}					
						
						uhm.remove(inId.getText());
						result.setText("삭제되었습니다.");
						inId.setText("");
						inName.setText("");
						inPhone.setText("");
						
						btnMod.setEnabled(false);
						btnDel.setEnabled(false);
					}else{
						JOptionPane.showMessageDialog(null, "삭제가 취소 되었습니다.");
					}
					
					
				}
			});
			btnDel.setBounds(213, 72, 116, 23);
		}
		return btnDel;
	}
	private JTextField getResult() {
		if (result == null) {
			result = new JTextField();
			result.setBounds(85, 104, 116, 21);
			result.setColumns(10);
		}
		return result;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("ID");
			lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel.setBounds(12, 14, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblName() {
		if (lblName == null) {
			lblName = new JLabel("Name");
			lblName.setHorizontalAlignment(SwingConstants.RIGHT);
			lblName.setBounds(12, 45, 57, 15);
		}
		return lblName;
	}
	private JLabel getLblPhone() {
		if (lblPhone == null) {
			lblPhone = new JLabel("Phone");
			lblPhone.setHorizontalAlignment(SwingConstants.RIGHT);
			lblPhone.setBounds(12, 76, 57, 15);
		}
		return lblPhone;
	}
	private JLabel getLblStatus() {
		if (lblStatus == null) {
			lblStatus = new JLabel("Status");
			lblStatus.setHorizontalAlignment(SwingConstants.RIGHT);
			lblStatus.setBounds(12, 107, 57, 15);
		}
		return lblStatus;
	}
	public void setMap(HashMap m){
		this.uhm=m;
		
	}
}
